package hadoop.mr08;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class W_mapper extends Mapper<LongWritable, Text, NullWritable,W_stuTable> {
    // 读取文本数据写入mysql
    NullWritable outkey = NullWritable.get();
    W_stuTable out_value = new W_stuTable();
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, NullWritable, W_stuTable>.Context context) throws IOException, InterruptedException {
        // 获取两个计数器 用于统计合法数据与非法数据
        Counter isnull = context.getCounter("CheckNull", "isNull");
        Counter notnull = context.getCounter("CheckNull", "notNull");

        String[] split = value.toString().split(",");
        if(split[0].equals("学号")==false){ // 去除第一行
            // 判断是否有缺失数据
            if(split.length>10){
                out_value.set(
                        split[0],
                        split[1],
                        split[2],
                        split[3],
                        split[4],
                        split[5],
                        split[6],
                        split[7],
                        split[8],
                        split[9],
                        split[10]
                );
                context.write(outkey,out_value);
                // 无却是值
                notnull.increment(1);
            }else {
                // 有缺失值
                isnull.increment(1);
            }
        }
    }
}
